<?php

namespace App\Model;
use PhalApi\Model\DataModel;
use App\Model\IdcardFromUnitTotal as Unit;

class IdcardFromUnitCheck extends DataModel
{
    public $tableName = 'idcard_from_unit_checker';
    protected function getTableName($id)
    {
        return $this->tableName;
    }
    //存入数据
    public function check_from_unit($data)
    {
        //重构新数组
        $new_data = array();
        foreach ($data as $k => $v) {
            if ($v) {
                $new_data[$k] = $v;
            }
        }
        //送检单位总表
        $Unit = new Unit();
        $Unit->total_from_unit(
            array(
                "from_unit" => $data['from_unit'],
                "unit" => $data['unit']
            )
        );
        //个人送检单位表
        if ($data['from_unit'] && $data['checker']) {
            //处理送检单位
            $where = array(
                "from_unit" => $data['from_unit'],
                "checker" => $data['checker'],
                "unit" => $data['unit'],
            );
            $new_data['update_time'] = date("Y-m-d H:i:s", time());
            //查询结果
            $info = $this->getORM()->select('id')->where($where)->fetchOne();
            //如果有信息,就返回相关数组
            if ($info) {
                $this->getORM()->where('id', $info['id'])->update($data);
            } else {
                $new_data['create_time'] = date("Y-m-d H:i:s", time());
                $this->getORM()->insert($new_data);
            }
        }
        //检测单位送检单位表
        if ($data['from_unit'] && $data['unit']) {
            //把送检单位,按检测单位归档
            $this->tableName = 'idcard_from_unit_by_unit';
            //处理送检单位
            $where = array(
                "from_unit" => $data['from_unit'],
                "unit" => $data['unit'],
            );
            $new_data['update_time'] = date("Y-m-d H:i:s", time());
            //查询结果
            $info = $this->getORM()->select('id')->where($where)->fetchOne();
            //如果有信息,就返回相关数组
            if ($info) {
                $this->getORM()->where('id', $info['id'])->update($data);
            } else {
                $new_data['create_time'] = date("Y-m-d H:i:s", time());
                $this->getORM()->insert($new_data);
            }
        }
    }
}





